using System;
using System.Runtime.Serialization;
using System.Data;
using Widgetsphere.Core.DataAccess;
using Acme.Sales.Business.Objects;
using Acme.Sales.Business.Objects.Components;
using Acme.Sales.Business.Views;
using Acme.Sales.Business.StoredProcedures;
using Acme.Sales.Domain.Objects;
using Acme.Sales.Domain.Views;
using Acme.Sales.Domain.StoredProcedures;

//Add the namespaces so they will always exist even if the objects do not
namespace Acme.Sales.Business.Views
{
	internal class X { }
}
namespace Acme.Sales.Domain.Views
{
	internal class X { }
}
namespace Acme.Sales.Business.Objects
{
	internal class X { }
}
namespace Acme.Sales.Business.Objects.Components
{
	internal class X { }
}
namespace Acme.Sales.Business.Objects.Composites
{
	internal class X { }
}
namespace Acme.Sales.Domain.Objects
{
	internal class X { }
}
namespace Acme.Sales.Domain.Objects.Components
{
	internal class X { }
}
namespace Acme.Sales.Business.Rules
{
	internal class X { }
}
namespace Acme.Sales.Business.StoredProcedures
{
	internal class X { }
}

namespace Acme.Sales.Business.SelectCommands
{
	internal class X { }
}

namespace Acme.Sales.Domain.StoredProcedures
{
	internal class X { }
}
namespace Acme.Sales.Business
{
		/// <summary>
		/// The container for all object collections.
		/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	[Serializable()]
	public class SubDomain : SubDomainBase
	{

		#region Constructors
		/// <summary>
		/// The constructor for the subdomain.
		/// </summary>
		public SubDomain(string modifier) : base(modifier){}
		/// <summary>
		/// The constructor for the subdomain.
		/// </summary>
		public SubDomain():base(){}
		#endregion 

		#region GetCollection

		/// <summary>
		/// Get a business collection in this subdomain
		/// </summary>
		public T GetCollection<T>() where T : IBusinessCollection
		{
			if (false) {}
			else if (typeof(T) == typeof(CustomerCollection))
				return (T)this[Collections.CustomerCollection];
			return default(T);
		}
		#endregion 

		#region SubDomainBase Implementation

		/// <summary>
		/// Deterines if the connection uses the web service or connects directly to the database.
		/// </summary>
		[Obsolete("This property no longer applies.")]
		protected override bool DirectConnect
		{
			get
			{
				return ConfigurationValues.GetInstance().DirectConnect;
			}
		}

		/// <summary>
		/// The web service URL end point.
		/// </summary>
		[Obsolete("This property no longer applies.")]
		protected override string DataAccessServiceUrl
		{
			get { return ConfigurationValues.GetInstance().DataAccessServiceUrl; }
		}

		/// <summary>
		/// Determines if any object in this container needs to be saved.
		/// </summary>
		public override bool IsDirty
		{
			get
			{
				foreach (DataTable dt in this.Tables)
				{
					foreach (DataRow dr in dt.Rows)
					{
						if (dr.RowState != DataRowState.Unchanged)
							return true;
					}
				}
				return false;
			}
		}

		/// <summary>
		/// Determines the connection string to the database.
		/// </summary>
		public override string ConnectionString
		{
			get { return ConfigurationValues.GetInstance().ConnectionString; }
		}

		/// <summary>
		/// Adds all object collections in this assembly to the subdomain.
		/// </summary>
		protected override void CreateStronglyTypedTables(System.Data.DataSet ds)
		{
			this.EnsureParentExistsOn = false;

			if(ds.Tables.Contains("CustomerCollection"))
			{
				DomainCustomerCollection tmpCollection = (DomainCustomerCollection)ds.Tables["CustomerCollection"];
				this.AddCollection(Collections.CustomerCollection);
				DomainCustomerCollection permanentCollection = (DomainCustomerCollection)this.GetDomainCollection(Collections.CustomerCollection);

			}


			this.EnsureParentExistsOn = true;
		}

		#endregion 

		#region Contains

		/// <summary>
		/// Returns the number of loaded items in the specified collection.
		/// </summary>
		internal bool Contains(Collections collection)
		{
			string tableName = collection.ToString();
			return this.Tables.Contains(tableName);
		}

		#endregion 

		#region GetDomainCollection

		/// <summary>
		/// Returns the specified collection as a strongly-typed object. If the collection is not part of the subdomain, it is added.
		/// </summary>
		internal DomainCollectionBase GetDomainCollection(Collections collection)
		{
			string tableName = collection.ToString();
			if(!this.Tables.Contains(tableName))
			{
				AddCollection(collection);
			}
			return base[collection.ToString()];
		}

		#endregion 

		#region AddCollection

		/// <summary>
		/// Adds a collection of the specified type to the subdomain.
		/// </summary>
		public void AddCollection(Collections collection)
		{
			bool oldEc = this.EnforceConstraints;
			this.EnforceConstraints = false;
			switch(collection)
			{
				case Collections.CustomerCollection: this.AddCollection(new DomainCustomerCollection());break;

			}
			this.EnsureParentRowsExist();
			this.EnforceConstraints = oldEc;
		}

		#endregion

		#region this[]

		/// <summary>
		/// Returns the specified collection in this subdomain.
		/// </summary>
		protected override IBusinessCollection this[Enum collection]
		{
			get
			{
				return (IBusinessCollection)this[(Collections)collection];
			}
		}

		/// <summary>
		/// Returns the specified collection in this subdomain.
		/// </summary>
		public IBusinessCollection this[Collections collection]
		{
			get
			{
				IBusinessCollection businessCollection = null;
				switch(collection)
				{
				case Collections.CustomerCollection: businessCollection = new CustomerCollection((DomainCustomerCollection)this.GetDomainCollection(collection));break;


				}
				return businessCollection;
			}
		}

	#endregion

	}

	#region Collections Enumeration
		/// <summary>
		/// An enumeration of all tables that can be held by this subdomain
		/// </summary>
		public enum Collections
		{
			/// <summary>
			/// Identifies set object set
			/// </summary>
			_NotSet = 0,
			/// <summary>
			/// Identifies the Customer table
			/// </summary>
			CustomerCollection = 1,
	}
	#endregion

}
